home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lib-src / yow.c < prev   
C/C++ Source or Header  |  1994-06-07  |  3KB  |  158 lines

  1. /*
  2.  * yow.c
  3.  * 
  4.  * Print a quotation from Zippy the Pinhead.
  5.  * Qux <Kaufman-David@Yale> March 6, 1986
  6.  * 
  7.  * With dynamic memory allocation.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <../src/paths.h>      /* For PATH_DATA.  */
  13.  
  14. #define BUFSIZE  80
  15. #define SEP      '\0'
  16.  
  17. #ifndef YOW_FILE
  18. #define YOW_FILE "yow.lines"
  19. #endif
  20.  
  21. #ifdef MSDOS
  22. #define rootrelativepath(rel) \
  23. ({\
  24.     static char res[BUFSIZE], *p;\
  25.     strcpy (res, argv[0]);\
  26.     p = res + strlen (res);\
  27.     while (p != res && *p != '/' && *p != '\\' && *p != ':') p--;\
  28.     strcpy (p + 1, "../");\
  29.     strcpy (p + 4, rel);\
  30.     &res;})
  31. #endif
  32.  
  33. main (argc, argv)
  34.      int argc;
  35.      char *argv[];
  36. {
  37.   FILE *fp;
  38.   char file[BUFSIZ];
  39.   void yow(), setup_yow();
  40.  
  41.   if (argc > 2 && !strcmp (argv[1], "-f"))
  42.     strcpy (file, argv[2]);
  43.   else
  44. #ifdef vms
  45.     sprintf (file, "%s%s", PATH_DATA, YOW_FILE);
  46. #else
  47.     sprintf (file, "%s/%s", PATH_DATA, YOW_FILE);
  48. #endif
  49.  
  50.   if ((fp = fopen(file, "r")) == NULL) {
  51.     perror(file);
  52.     exit(1);
  53.   }
  54.  
  55.   /* initialize random seed */
  56.   srand((int) (getpid() + time((long *) 0)));
  57.  
  58.   setup_yow(fp);
  59.   yow(fp);
  60.   fclose(fp);
  61.   exit(0);
  62. }
  63.  
  64. static long len = -1;
  65. static long header_len;
  66.  
  67. #define AVG_LEN 40        /* average length of a quotation */
  68.  
  69. /* Sets len and header_len */
  70. void
  71. setup_yow(fp)
  72.      FILE *fp;
  73. {
  74.   int c;
  75.  
  76.   /* Get length of file */
  77.   /* Because the header (stuff before the first SEP) can be very long,
  78.    * thus biasing our search in favor of the first quotation in the file,
  79.    * we explicitly skip that. */
  80.   while ((c = getc(fp)) != SEP) {
  81.     if (c == EOF) {
  82.       fprintf(stderr, "File contains no separators.\n");
  83.       exit(2);
  84.     }
  85.   }
  86.   header_len = ftell(fp);
  87.   if (header_len > AVG_LEN)
  88.     header_len -= AVG_LEN;    /* allow the first quotation to appear */
  89.     
  90.   if (fseek(fp, 0L, 2) == -1) {
  91.     perror("fseek 1");
  92.     exit(1);
  93.   }
  94.   len = ftell(fp) - header_len;
  95. }
  96.  
  97.  
  98. /* go to a random place in the file and print the quotation there */
  99. void
  100. yow (fp)
  101.      FILE *fp;
  102. {
  103.   long offset;
  104.   int c, i = 0;
  105.   char *buf;
  106.   unsigned int bufsize;
  107.   char *malloc(), *realloc();
  108.  
  109.   offset = rand() % len + header_len;
  110.   if (fseek(fp, offset, 0) == -1) {
  111.     perror("fseek 2");
  112.     exit(1);
  113.   }
  114.  
  115.   /* Read until SEP, read next line, print it.
  116.      (Note that we will never print anything before the first separator.)
  117.      If we hit EOF looking for the first SEP, just recurse. */
  118.   while ((c = getc(fp)) != SEP)
  119.     if (c == EOF) {
  120.       yow(fp);
  121.       return;
  122.     }
  123.  
  124.   /* Skip leading whitespace, then read in a quotation.
  125.      If we hit EOF before we find a non-whitespace char, recurse. */
  126.   while (isspace(c = getc(fp)))
  127.     ;
  128.   if (c == EOF) {
  129.     yow(fp);
  130.     return;
  131.   }
  132.  
  133.   bufsize = BUFSIZE;
  134.   buf = malloc(bufsize);
  135.   if (buf == (char *)0) {
  136.     fprintf(stderr, "can't allocate any memory\n");
  137.     exit (3);
  138.   }
  139.  
  140.   buf[i++] = c;
  141.   while ((c = getc(fp)) != SEP && c != EOF) {
  142.     buf[i++] = c;
  143.     
  144.     if (i == bufsize-1) {
  145.       /* Yow! Is this quotation too long yet? */
  146.       bufsize *= 2;
  147.       buf = realloc(buf, bufsize);
  148.       if (buf == (char *)0) {
  149.     fprintf(stderr, "can't allocate more memory\n");
  150.     exit (3);
  151.       }
  152.     }
  153.   }
  154.   buf[i++] = 0;
  155.   printf("%s\n", buf);
  156. }
  157.  
  158.